package com.example.demo;

import com.example.demo.entity.grade;
import com.example.demo.entity.users;
import com.example.demo.mapper.GradeMapper;
import com.example.demo.mapper.IntegralMapper;
import com.example.demo.mapper.usersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class SaticScheduleTask {
    /**
     * 24时(h)=86400秒(s)
     * 每隔24小时更新一下等级
     * 1小时等于3600 s
     **/
    @Autowired
    usersMapper usersMapper;
    @Autowired
    IntegralMapper integralMapper;
    @Autowired
    GradeMapper gradeMapper;

    @Scheduled(fixedRate = 60000  )
    private void configureTasks() {
        List<users> list = usersMapper.selsectuserAll();
        List<grade> gradeList = gradeMapper.selestgrade();
        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < gradeList.size() - 1; j++) {
                if (integralMapper.selectinterfaceBymailbox(list.get(i).getMailbox()).getIntegral() >=
                        gradeList.get(j).getIntegral()
                        && integralMapper.selectinterfaceBymailbox(list.get(i).getMailbox()).getIntegral() <
                        gradeList.get(j + 1).getIntegral()
                ) {
                    usersMapper.updategrade(gradeList.get(j).getGrade(), list.get(i).getMailbox());
                }
            }
            if (integralMapper.selectinterfaceBymailbox(list.get(i).getMailbox()).getIntegral() >=
                    gradeList.get(gradeList.size() - 1).getIntegral()) {
                usersMapper.updategrade(gradeList.get(gradeList.size() - 1).getGrade(), list.get(i).getMailbox());
            }
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
    }
}
